<?php

namespace App\Manage;


use App\Models\SiteInfo;
use App\Models\User;
use App\Service\Base\Manage;

class EventManage extends Manage
{

    // 基本配置
    public function initialize()
    {
        parent::initialize();

        $this->model = 'Event';
    }

    // 列表
    // 添加
    // 修改

    // 软删除
    protected function toSoft()
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('请指定删除记录');
        }

        $where = $this->delWhere();
        if($where === false){
            return $this->_returnFail($this->error);
        }

        $model  = $this->checkModel($this->model);
        $result = $model->isCache(true)->oneEdit($where, ['verify' => -1]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 推荐
    public function onPropose()
    {
        $result = $this->toChange('is_propose', [0, 1]);

        return $result;
    }


    // 审核
    public function onVerify()
    {
        $opt    = $this->option();
        $_value = $opt->getData('value', null, 'intval');
        $_id    = $opt->getData('id', 0, 'intval');
        if(is_null($_value)){
            return $this->_returnFail('状态必须填写！！');
        }
        if(empty($_id)){
            return $this->_returnFail('请指定操作记录！！');
        }
        if(!in_array($_value, [1, 2])){
            return $this->_returnFail('审核状态必须为2或1！！');
        }

        $model  = $this->checkModel($this->model);
        $result = $model->oneEdit(['id' => $_id], ['verify' => $_value]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 评论列表


    // 列表查询
    protected function listWhere()
    {
        $option  = $this->option();
        $where   = [['verify', '>=', 0]];
        $bang    = [];
        $c_id    = $option->getData('course_id', 0, 'intval');
        $uid     = $option->getData('uid', 0, 'intval');
        $s_id    = $option->getData('student_id', 0, 'intval');
        $is_prop = $option->getData('is_propose', null, 'intval');
        $_verify = $option->getData('verify', null, 'intval');
        $_type   = $option->getData('event_type', null, 'trim');
        $name    = $option->getData('event_name', null, 'trim');
        $keyword = $option->getData('keyword', null, 'trim');
        if($c_id){
            $bang[]  = 'course_id';
            $where[] = ['course_id', '=', $c_id];
        }
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if($s_id){
            $bang[]  = 'student_id';
            $where[] = ['student_id', '=', $s_id];
        }
        if(!is_null($is_prop)){
            $bang[]  = 'is_propose';
            $where[] = ['is_propose', '=', $is_prop];
        }
        if(!is_null($_verify)){
            $bang[]  = 'verify';
            $where[] = ['verify', '=', $_verify];
        }
        if($_type){
            $bang[]  = 'event_type';
            $where[] = ['event_type', '=', $_type];
        }
        if($name){
            $bang    = false;
            $where[] = ['event_name', 'like', "%".$name."%"];
        }
        if($keyword){
            $bang    = false;
            $where[] = $this->orWhere('event_name|description|address', 'like', $keyword);
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }


    // 添加之前
    protected function _before_add()
    {
        $opt = $this->option();
        $uid = $opt->getData("uid", null, "intval");
        // 获取认证编号
        $user = (new User())->isCache(true)->oneFind(['id' => $uid]);
        if(empty($user)){
            return $this->_returnFail('你选择的用户不存在！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你选择的用户没有认证！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你选择的用户没有认证！');
        }
        if($user['verify'] != 1){
            return $this->_returnFail('你选择的用户认证未通过！');
        }

        $auth_id = $user['auth_id'];
        $data    = [
            //'id'          => $opt->getData("id", null, "intval"),
            'uid'          => $uid,
            'auth_id'      => $auth_id,
            'event_type'   => $opt->getData("event_type", null, "trim"),
            'event_name'   => $opt->getData("event_name", null, "trim"),
            'organ_price'  => $opt->getData("organ_price", null, "trim"),
            'person_price' => $opt->getData("person_price", null, "trim"),
            'is_refund'    => $opt->getData("is_refund", null, "intval"),
            'dead_time'    => $opt->getData("dead_time", null, "intval"),
            'start_time'   => $opt->getData("start_time", null, "intval"),
            'end_time'     => $opt->getData("end_time", null, "intval"),
            'city'         => $opt->getData("city", null, "trim"),
            'address'      => $opt->getData("address", null, "trim"),
            'description'  => $opt->getData("description", null, "trim"),
            'cover'        => $opt->getData("cover", null, "trim"),
            'images'       => $opt->getData("images", "[]", "trim"),
            //'images'       => $opt->getData("images", "[]", "json_encode"),
            'is_propose'   => $opt->getData("is_propose", null, "intval"),
            'verify'       => $opt->getData("verify", null, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        // 如果有市场价 和 成本价的时候
        // 如果有市场价 和 成本价的时候
        if($data['person_price'] || $data['organ_price']){
            if($data['person_price'] < $data['organ_price']){
                return $this->_returnFail('个人价格不能低于机构价格！');
            }
        }

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt     = $this->option();
        $uid     = $opt->getData("uid", null, "intval");
        $auth_id = null;
        // 获取认证编号
        if($uid){
            $user = (new User())->isCache(true)->oneFind(['id' => $uid]);
            if(empty($user)){
                return $this->_returnFail('你选择的用户不存在！');
            }
            if(empty($user['is_auth'])){
                return $this->_returnFail('你选择的用户没有认证！');
            }
            if(empty($user['is_auth'])){
                return $this->_returnFail('你选择的用户没有认证！');
            }
            if($user['verify'] != 1){
                return $this->_returnFail('你选择的用户认证未通过！');
            }

            // 重新定义
            $auth_id = $user['auth_id'];
        }

        // 组合数据
        $data = [
            //'id'          => $opt->getData("id", null, "intval"),
            'uid'          => $uid,
            'auth_id'      => $auth_id,
            'event_type'   => $opt->getData("event_type", null, "trim"),
            'event_name'   => $opt->getData("event_name", null, "trim"),
            'organ_price'  => $opt->getData("organ_price", null, "trim"),
            'person_price' => $opt->getData("person_price", null, "trim"),
            'is_refund'    => $opt->getData("is_refund", null, "intval"),
            'dead_time'    => $opt->getData("dead_time", null, "strtotime"),
            'start_time'   => $opt->getData("start_time", null, "intval"),
            'end_time'     => $opt->getData("end_time", null, "intval"),
            'city'         => $opt->getData("city", null, "trim"),
            'address'      => $opt->getData("address", null, "trim"),
            'description'  => $opt->getData("description", null, "trim"),
            'cover'        => $opt->getData("cover", null, "trim"),
            //'images'       => $opt->getData("images", "[]", "json_encode"),
            'images'       => $opt->getData("images", "[]", "trim"),
            'is_propose'   => $opt->getData("is_propose", null, "intval"),
            'verify'       => $opt->getData("verify", null, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        // 如果有市场价 和 成本价的时候
        if($data['person_price'] || $data['organ_price']){
            if($data['person_price'] < $data['organ_price']){
                return $this->_returnFail('个人价格不能低于机构价格！');
            }
        }

        return $data;
    }

}


